package com.laifeng.sopcastsdk.stream.sender.local;

import android.os.Environment;
import android.util.Log;

import com.laifeng.sopcastsdk.local.LocalVideoFileManager;
import com.laifeng.sopcastsdk.stream.sender.Sender;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @Title: LocalSender
 * @Package com.laifeng.sopcastsdk.stream.sender.local
 * @Description:
 * @Author Jim
 * @Date 16/9/18
 * @Time 下午5:10
 * @Version
 */
public class LocalSender implements Sender {

    private static final long FILE_MAX_SIZE_BYTE = 8 * 1024 * 1024;
    private long mSendBytes = 0;

    private File mCurFile = null;
    private FileOutputStream mOutStream;
    private static BufferedOutputStream mBuffer;

    @Override
    public void start() {
        mCurFile = transferNewFile();
    }


    public boolean onTransferFile() {
        transferNewFile();
        return true;
    }

    @Override
    public void onData(byte[] data, int type) {
        if (mBuffer != null) {
            try {
                mBuffer.write(data);
                mBuffer.flush();
//                mSendBytes += data.length;
//                long needBytes = FILE_MAX_SIZE_BYTE - mSendBytes;
//                if (needBytes < data.length) {
//                    mSendBytes = 0;
//                    Log.e("YJQ", "finishFileName>>>" + mCurFile.getName());
//                    Log.e("YJQ", "finishFileSize>>>" + mCurFile.length());
//                    mCurFile = transferNewFile();
//                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void stop() {
        if (mBuffer != null) {
            try {
                mBuffer.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mBuffer = null;
        }

        if (mOutStream != null) {
            try {
                mOutStream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mBuffer = null;
            mOutStream = null;
        }
    }

    private File transferNewFile() {
        File emptyFile = LocalVideoFileManager.getInstance().getEmptyFile(FILE_MAX_SIZE_BYTE);
        if (emptyFile.exists()) {
            emptyFile.delete();
        }

        Log.e("YJQ", "newFile>>>" + emptyFile.getName());

        try {
            if (mOutStream != null) {
                mOutStream.close();
            }

            if (mBuffer != null) {
                mBuffer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            mOutStream = null;
            mBuffer = null;
        }

        try {
            emptyFile.createNewFile();
            mOutStream = new FileOutputStream(emptyFile);
            mBuffer = new BufferedOutputStream(mOutStream);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return emptyFile;
    }
}
